import axios from "axios";
import { Modal } from "antd";
const context = import.meta.env.VITE_SERVER_URL;




// 创建一个基于基本配置的axios实例
const http = axios.create({
  baseURL: context,
  timeout: 1000,
  headers: {
    "Content-Type": "application/json;charset=UTF-8",
  },
});

// 添加请求拦截器
http.interceptors.request.use((config) => {
  console.log("发送请求",config);
  // Modal.warning({
  //   content: "发送请求拦截",
  //   centered: true,
  //   okText: "确认",
  //   onOk() {
  //     // 弹出登录弹框
  //   },
  // });
  return config;
});
// 添加响应拦截器
http.interceptors.response.use(
  (response) => {
    console.log("接收响应",response);

    if(response.status != 200 || !response.data || response.data.code !== '0'){ // 非200
      Modal.error({
        content: "操作失败",
        centered: true,
        okText: "确认"
      });
    }else{
      return response.data;
    }

  },
  (error) => {
    console.log("TCL: error", error);
    const msg = error.Message !== undefined ? error.Message : "";
    Modal.warning({
        content: "发生错误",
        centered: true,
        okText: "确认",
        onOk() {
          // 弹出登录弹框
        },
      });
    return Promise.reject(error);
  }
);






// 创建一个基于基本配置的axios实例
const downloadHttp = axios.create({
  baseURL: context,
  timeout: 1000,
  headers: {
    "Content-Type": "application/json;charset=UTF-8",
  },
  responseType:'blob'
});

// 添加请求拦截器
downloadHttp.interceptors.request.use((config) => {
  console.log("发送请求",config);
  // Modal.warning({
  //   content: "发送请求拦截",
  //   centered: true,
  //   okText: "确认",
  //   onOk() {
  //     // 弹出登录弹框
  //   },
  // });
  return config;
});
// 添加响应拦截器
downloadHttp.interceptors.response.use(
  (response) => {
    console.log("接收响应",response);
    if(response.status != 200 || !response.data ){ // 非200
      Modal.error({
        content: "操作失败",
        centered: true,
        okText: "确认"
      });
    }else{
      return response.data;
    }

  },
  (error) => {
    console.log("TCL: error", error);
    const msg = error.Message !== undefined ? error.Message : "";
    Modal.warning({
        content: "发生错误",
        centered: true,
        okText: "确认",
        onOk() {
          // 弹出登录弹框
        },
      });
    return Promise.reject(error);
  }
);

export {downloadHttp};
export default http;